require.config({
    paths: {
        'columntoggle': '../addons/facrm/js/columntoggle',
    }
});
define(['jquery', 'bootstrap', 'backend', 'table', 'form','columntoggle'], function ($, undefined, Backend, Table, Form,Columntoggle) {
//本地缓存定义
    var Controller = {
        index: function () {
            var scene_id="";
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/index',
                    add_url: 'facrm/customer/index/add',
                    edit_url: 'facrm/customer/index/edit',
                    del_url: 'facrm/customer/index/del',
                    multi_url: '',
                    table: '',
                }
            });
            var table = $("#customer");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                if (from_type == "need") {
                    //如果是需要跟进的客户
                    $(".commonsearch-table").removeClass("hidden");
                    $("input[name='next_time']").val(Moment().year(Moment().year() - 10).startOf('year').format('YYYY-MM-DD H:mm:ss') + ' - ' + Moment().endOf('day').format('YYYY-MM-DD H:mm:ss'));
                } else if (from_type == "expire") {
                    $(".commonsearch-table select[name=expire_type]").val(1);
                    $(".commonsearch-table").removeClass("hidden");
                }
                $(".commonsearch-table select[name=scene_id]").val(1);
            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: 'ID'},
                {
                    field: 'scene_id',
                    title: __('客户场景'),
                    visible: false,
                    formatter: function (v, r) {
                        return scene_list[v];
                    }, searchList: scene_list
                },
                {
                    field: 'name',
                    title: __('客户名称'),
                    operate: "LIKE",
                    align: 'left',
                    customField: 'tags',
                    formatter: function (value, row, index) {
                        value='<a href="javascript:void(0);" data-url="facrm/customer/index/detail/ids/' + row.id
                            +'" data-area="[&quot;100%&quot;,&quot;100%&quot;]" class="btn-dialog" data-title="'+ value+'">'+ value+'</a>';
                        return value +
                            '<div >' + Table.api.formatter.flag.call(this, row['tags'], row, index) + '</div>';
                    }
                },

                {
                    field: 'tags',
                    title: __('标签'),
                    operate: 'find_in_set',
                    visible: false,
                    formatter: Table.api.formatter.flag
                },
                {
                    field: 'level', title: __('客户级别'), formatter: function (v, r) {
                        return levelList[v];
                    }, searchList: levelList
                },
                {
                    field: 'industry', title: __('行业'), formatter: function (v, r) {
                        return industryList[v];
                    }, searchList: industryList
                },
                {
                    field: 'source', title: __('来源'), formatter: function (v, r, index) {
                        return sourceList[v];
                    }, searchList: sourceList
                },
                {field: 'telephone', title: __('电话'), operate: 'LIKE'},
                {field: 'mobile', title: __('手机'), operate: 'LIKE'},

                {field: 'remark', title: __('备注'), operate: 'LIKE'},
                {field: 'website', title: __('网址'), operate: 'LIKE'},
                {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {field: 'owner_user.nickname', title: __('负责人'), operate: false, search: false},
                {field: 'owner_user_id', title: __('负责人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {
                    field: 'next_time',
                    title: __('下次跟进'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'follow_time',
                    title: __('最近跟进'),
                    visible: false,
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
					sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'expire_type',
                    title: __('将要过期'),
                    visible: false,
                    searchList: {'1': "将要过期客户"}
                },
                {
                    field: 'create_time',
                    title: __('创建时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'collect_time',
                    title: __('领取时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'update_time',
                    title: __('更新时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
                    visible: false,
                    extend: 'autocomplete="off"'
                },

                {
                    field: 'deal_status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status,
                    searchList: {1: __('已成交'), 0: __('未成交')}
                },
                {
                    field: 'operate',
                    title: __('Operate'),
                    table: table,
                    width: "180px",
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate,
                    buttons: [
                        {
                            name: 'record',
                            text: '跟进',
                            title: function ( row) {
                                return '跟进客户-'+row.name;
                            },
                            icon: 'fa fa-commenting-o',
                            classname: 'btn btn-xs btn-danger btn-addtabs',
                            url: 'facrm/customer/record/add',
                        },
                        {
                            name: 'discard',
                            text: '公海',
                            title: '公海',
                            url: 'facrm/customer/index/discard',
                            confirm: "确认把该客户放入公海？",
                            icon: 'fa fa-close',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return true;
                                return  false;
                            }

                        },
                        {
                            name: 'del',
                            url:"facrm/customer/index/del",
                            confirm:"确认把该客户删除？",
                            icon: 'fa fa-trash',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return true;
                                return  false;
                            }
                        },

                        {
                            name: 'share',
                            title: '分享客户',
                            url:"facrm/customer/index/share",
                            icon: 'fa fa-share-square-o',
                            classname: 'btn btn-xs btn-success  btn-dialog',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[2])==-1) return true;
                                return  false;
                            }
                        },

                        {
                            name: 'sdel',

                            url: 'facrm/customer/index/sdel',
                            confirm:"确认把该客户从分享删除?",
                            icon: 'fa fa-close',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return false;
                                return  true;
                            }
                        },
                    ]
                },

            ];

            if (Config.fields && Object.keys(Config.fields).length > 0) {
                var fields = JSON.parse(Config.fields);
                var start = 19;
                for (var i = 0; i < fields.length; i++) {
                    if (fields[i].hasOwnProperty('formatter'))
                        fields[i].formatter = eval(fields[i].formatter);
                    if (fields[i].hasOwnProperty('events'))
                        fields[i].events = eval(fields[i].events);

                    columnss.splice(start + i, 0, fields[i]);
                }
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    columnss
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer","customer_table")
                },
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                scene_id=value;
                return false;
            });
            // 批量放入公海事件
            $("#toolbar").on('click', '.btn-discard', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定放入公海 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });

            // 批量删除事件
            $("#toolbar").on('click', '.btn-dels', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定删除 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });

            // 批量转移
            $("#toolbar").on('click', '.btn-divert', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                Fast.api.open(url + "/ids/" + ids, title, data);
            });

            //批量发邮件
            $(".btn-send-emails").click(function () {
                var that = this;
                var title = $(that).data('title') || $(that).attr("title") || __('批量发邮件');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title,data.url;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                data.typesid=ids;
                parent.Fast.api.open(url+"?data="+encodeURI(JSON.stringify(data)), title, data);
            });
			// 合并
            $("#toolbar").on('click', '.btn-merge', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                Fast.api.open(url + "/ids/" + ids, title, data);
            });

        },

        reduplicate:function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/reduplicate',
                    add_url: 'facrm/customer/index/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',
                }
            });
            var table = $("#customer");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: 'ID'},

                {
                    field: 'name',
                    title: __('客户名称'),
                    operate: "LIKE",
                    align: 'left',
                    customField: 'tags',
                    formatter: function (value, row, index) {
                        return value +
                            '<div >' + Table.api.formatter.flag.call(this, row['tags'], row, index) + '</div>';
                    }
                },
                {
                    field: 'mobile',
                    title: __('手机'),
                    operate: "LIKE",
                    align: 'left'
                },
                {
                    field: 'tags',
                    title: __('标签'),
                    operate: 'find_in_set',
                    visible: false,
                    formatter: Table.api.formatter.flag
                },

                {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {field: 'owner_user.nickname', title: __('负责人'), operate: false, search: false},
                {field: 'owner_user_id', title: __('负责人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},

                {
                    field: 'create_time',
                    title: __('创建时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'follow_time',
                    title: __('跟进时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },


                {
                    field: 'deal_status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status,
                    searchList: {1: __('已成交'), 0: __('未成交')}
                }

            ];

        
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
				pagination: false,
                columns: [
                    columnss
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer","customer_table")
                },
                searchFormVisible:true, //打开搜索
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                return false;
            });


        },

        add: function () {
            Controller.api.bindevent();
        },
        divert: function () {
            Controller.api.bindevent();
        },
		merge: function () {
            Controller.api.bindevent();
            $(document).on('click', ".btn-submit", function () {
               var index= Layer.confirm(
                    __('您确定要合并吗?数据不可撤回！请谨慎使用！'),
                    {icon: 3, title: __('Warning'), shadeClose: true},
                    function (index) {
                        Form.api.submit($("form[role=form]"),function () {
                            parent.$("#customer").bootstrapTable("refresh");
                            parent.Layer.closeAll();
                            return true;
                        },function () {
                            index && Layer.close(index);
                        });
                    }
                );

            });
        },
        edit: function () {
            Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/record/index/customer_id/' + customer_id,
                    contact_url: 'facrm/customer/contacts/index/customer_id/' + customer_id,
                    business_url: 'facrm/business/index/index/customer_id/' + customer_id,
                    contract_url: 'facrm/customer/index/contractlist/customer_id/' + customer_id,
                    receivables_url: 'facrm/customer/index/receivableslist/customer_id/' + customer_id,
                    files_url: 'facrm/customer/record/files/customer_id/' + customer_id,
                }
            });
            var table = $("#tableflow");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
           
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar1',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'content', title: __('content'), operate: 'LIKE'},
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
							extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'record_type', title: __('跟进类型'), formatter: function (v, r) {
                                return record_type_list[v];
                            }, searchList: record_type_list
                        },

                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            //联系人
            var contact = $("#contact");
            contact.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("#forum select[name=scene_id]").val(4);
            });
            contact.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.contact_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar2',
                columns: [
                    [
                        {
                            field: 'operate', title: __('Operate'), table: contact,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit1',
                                    text: '',
                                    title: '修改联系人',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: 'facrm/customer/contacts/edit',

                                },
                                {
                                    name: 'del1',
                                    text: '',
                                    title: '删除',
                                    confirm: "确认删除联系人？",
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger  btn-ajax',
                                    url: 'facrm/customer/contacts/del',
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'scene_id', title: __('场景'), visible: false, searchList: {4: "全部"}},
                        {field: 'name', title: __('姓名')},
                        {field: 'mobile', title: __('手机'), operate: 'LIKE', formatter: function (value, row, index) {
                                if (!value) return '-';
                                return '<a href="javascript:;" class="btn btn-success cloudcall btn-xs" data-field="mobile" data-typeid="'+
                                    row.id+'" data-type="customer_contacts" data-name="'+row.name+'"><i class="fa fa-phone"></i> ' +value + '</a>';
                            }},
                        {field: 'telephone', title: __('电话'), operate: 'LIKE', formatter: function (value, row, index) {
                                if (!value) return '-';
                                return '<a href="javascript:;" class="btn btn-success cloudcall btn-xs" data-field="telephone" data-typeid="'+
                                    row.id+'" data-type="customer_contacts" data-name="'+row.name+'"><i class="fa fa-phone"></i> ' +value + '</a>';
                            }},
                        {field: 'email', title: __('邮箱')},
                        {field: 'wechat', title: __('微信')},
                        {field: 'post', title: __('职务')},
                        {field: 'sex', title: __('性别')},
                        {field: 'decision', title: __('决策人')},
                        {field: 'detail_address', title: __('地址')},
                        {field: 'remark', title: __('备注')},
                        {
                            field: 'next_time',
                            title: __('下次联系'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        }


                    ]
                ],
            });
            // 初始化表格
            contact.bootstrapTable();
            Table.api.bindevent(contact);//当内容渲染完成后

            //商机
            var business = $("#t-business");
            business.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("#business select[name=scene_id]").val(7);
            });

            business.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.business_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-business',
                columns: [
                    [

                        {field: 'scene_id', title: __('场景'), visible: false, searchList: {7: "全部"}},
                        {
                            field: 'operate', title: __('Operate'), table: business,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'record',
                                    text: '跟进',
                                    title: '跟进',
                                    icon: 'fa fa-commenting-o',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'facrm/business/record/add',
                                },
                                {
                                    name: 'edit1',
                                    text: '',
                                    title: '修改商机',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: 'facrm/business/index/edit',

                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        {
                    field: 'name',
                    title: __('商机名称'),
                    operate: "LIKE",
                    align: 'left',
                    formatter: function (value, row, index) {
                        value='<a href="javascript:void(0);" data-url="facrm/business/index/edit/ids/' + row.id
                            +'" data-area="[&quot;100%&quot;,&quot;100%&quot;]" class="btn-dialog" data-title="'+ row.name+'">'+ row.name+'</a>';
                        return value;
                    }},
                        {field: 'money', title: __('客户预算'), operate: 'BETWEEN', sortable: true},
                        {field: 'total_price', title: __('产品价格'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'deal_time',
                            title: __('预计成交'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
								 extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'update_time',
                            title: __('更新时间'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'is_end',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('已成交'), 0: __('未成交'), 2: __('失败'), 3: __('无效')}

                        },

                    ]
                ],
            });
            // 初始化表格
            business.bootstrapTable();
            Table.api.bindevent(business);//当内容渲染完成后

            //合同
            var contract = $("#t-contract");
            contract.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.contract_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-contract',
                columns: [
                    [
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: contract,
                            buttons: [
                                {
                                    name: 'payurl',
                                    text: '',
                                    title: __('线上续费收款码'),
                                    icon: 'fa fa-qrcode',
                                    classname: 'btn btn-xs btn-danger  btn-dialog',
                                    url: 'facrm/contract/index/payurl',
                                    hidden:function (row) {
                                        if (row.check_status == undefined||row.check_status!=2){
                                            return true;
                                        }
                                        if (!Config.payConfig||Config.payConfig.online_pay!='1'){
                                            return true;
                                        }
                                        //判断是否逾期
                                        var newtime = Math.round(new Date().getTime()/1000).toString();
                                        if (newtime<(row.end_time-(Config.payConfig.renew_day*86400))){
                                            return true;
                                        }
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'receivables',
                                    text: '详情',
                                    title: __('合同详情'),
                                    icon: '',
                                    classname: 'btn btn-info btn-dialog btn-xs',
                                    url: function(row){
                                        return 'facrm/customer/index/contract/customer_id/'+row.customer_id+'/ids/'+row.id;
                                    },
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        },
                        {field: 'number', title: __('合同编号'), operate: "LIKE"},
                        {field: 'name', title: __('合同名称'), operate: "LIKE"},
                        {field: 'money', title: __('合同金额'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'start_time',
                            title: __('开始时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            datetimeFormat: "YYYY-MM-DD",
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'end_time',
                            title: __('结束时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            datetimeFormat: "YYYY-MM-DD",
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {field: 'order_admin.nickname', title: __('签约人'), operate: false, search: false},
                        {field: 'order_admin_id', title: __('签约人'), visible: false},
                        {
                            field: 'check_status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('待审核'), 1: __('审核中'), 2: __('审核通过'), 3: __('审核未通过')}
                        },
                    ]
                ],
            });
            // 初始化表格
            contract.bootstrapTable();
            Table.api.bindevent(contract);//当内容渲染完成后

            //回款
            var receivables = $("#t-receivables");
            receivables.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.receivables_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-receivables',
                columns: [
                    [
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: receivables,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'payurl',
                                    text: '',
                                    title: __('线上收款码'),
                                    icon: 'fa fa-qrcode',
                                    classname: 'btn btn-success btn-dialog btn-xs',
                                    url: 'facrm/contract/receivables/payurl',
                                    hidden:function (row) {
                                        if (row.pay_type == 1){
                                            return true;
                                        }
                                        if (row.pay_status == undefined||row.pay_status==1){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'receivables',
                                    text: '详情',
                                    title: __('回款详情'),
                                    icon: '',
                                    classname: 'btn btn-info btn-dialog btn-xs',
                                    url: function(row){
                                        return 'facrm/customer/index/receivables/customer_id/'+row.customer_id+'/ids/'+row.id;
                                    },
                                },

                            ],
                        },
                        {field: 'number', title: __('回款编号'), operate: "LIKE"},
                        {field: 'contract.number', title: __('合同编号'), operate: "LIKE"},
                        {field: 'contract.name', title: __('合同名称'), operate: "LIKE"},
                        {field: 'money', title: __('回款金额'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'check_status',
                            title: __('Status'),
                            formatter: function (value, row, index) {
                                var check_status={0: __('待审核'), 1: __('审核中'), 2: __('审核通过'),3: __('审核未通过')};
                                var pay_status={1:'已付款',0:'未付款'};
                                var retrunstr='';
                                if(row['pay_type']==2){
                                    retrunstr='(在线'+pay_status[row['pay_status']]+')';
                                }else if(row['pay_type']==3){
                                    retrunstr='(续费)';
                                }
                                return check_status[row['check_status']]+retrunstr;
                            }

                        },

                    ]
                ],
            });
            // 初始化表格
            receivables.bootstrapTable();
            Table.api.bindevent(receivables);//当内容渲染完成后

            //附件
            var files = $("#t-files");
            files.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.files_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-files',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 1,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: false,
                columns: [
                    [
                        {field: 'url', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },

                    ]
                ],
            });


        },
        detail: function () {
            Controller.edit();
        },
        common: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/common',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#customer_common");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
               
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('客户名称'), operate: "LIKE",   align: 'left'},
                        {
                            field: 'level', title: __('客户级别'), formatter: function (v, r) {
                                return levelList[v];
                            }, searchList: levelList
                        },
                        {
                            field: 'industry', title: __('行业'), formatter: function (v, r) {
                                return industryList[v];
                            }, searchList: industryList
                        },
                        {
                            field: 'source', title: __('来源'), formatter: function (v, r) {
                                return sourceList[v];
                            }, searchList: sourceList
                        },
                        {field: 'telephone', title: __('电话'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机'), operate: 'LIKE'},

                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {field: 'website', title: __('网址'), operate: 'LIKE'},
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
							extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'update_time',
                            title: __('更新时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'deal_status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('已成交'), 0: __('未成交')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'receive',
                                    text: '领取',
                                    title: '领取',
                                    icon: 'fa fa-check',
                                    url: 'facrm/customer/index/receive',
                                    classname: 'btn btn-xs btn-success  btn-ajax',
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        },

                    ]
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer_common")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer_common","customer_common_table")
                },
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 批量领取事件
            $("#toolbar").on('click', '.btn-receive', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定领取 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });
            // 批量转移
            $("#toolbar").on('click', '.btn-divert', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length > 10) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                //循环弹出多个编辑框
                Fast.api.open(url + "/ids/" + ids, title, data);


            });

        },
        import: function () {
            Form.api.bindevent($("form[role=form]"),function (data) {
                if (data.errpath){
                    window.open(data.errpath)
                }
            });
        },
        //合同详情
        contract:function(){
            Form.api.bindevent($("form[role=form]"));
            if ("undefined" == typeof contract_id) {
                contract_id = 0;
            }
            Fast.api.ajax({
                type: 'GET',
                url: 'facrm/fields/get_fields_html',
                data: {source: 'contract', 'id': contract_id}
            }, function (data) {
                $("#extend").html(data.html);
                Form.api.bindevent($("#extend"));
                return false;
            },function () {
                return false;
            });
        },

        //回款详情
        receivables:function(){
            Form.api.bindevent($("form[role=form]"));
            if ("undefined" == typeof ids) {
                ids = 0;
            }
            Fast.api.ajax({
                type: 'GET',
                url: 'facrm/fields/get_fields_html',
                data: {source: 'contract_receivables', 'id': ids}
            }, function (data) {
                $("#extend").html(data.html);
                Form.api.bindevent($("#extend"));
                return false;
            },function () {
                return false;
            });
        },

        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/recyclebin',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $(".commonsearch-table select[name=scene_id]").val(1);
            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: 'ID'},
                {field: 'name', title: __('客户名称'), operate: "LIKE"},

                {field: 'telephone', title: __('电话'), operate: 'LIKE'},
                {field: 'mobile', title: __('手机'), operate: 'LIKE'},

                {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
						extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {field: 'owner_user.nickname', title: __('负责人'), operate: false, search: false},
                {field: 'owner_user_id', title: __('负责人'), visible: false,addclass: 'selectpage',
						extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {
                    field: 'delete_time',
                    title: __('删除时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'deal_status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status,
                    searchList: {1: __('已成交'), 0: __('未成交')}
                },
                {
                    field: 'operate',
                    width: '130px',
                    title: __('Operate'),
                    table: table,
                    events: Table.api.events.operate,
                    buttons: [
                        {
                            name: 'Restore',
                            text: __('Restore'),
                            classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                            icon: 'fa fa-rotate-left',
                            url: 'facrm/customer/index/restore',
                            refresh: true
                        },
                        {
                            name: 'Destroy',
                            text: __('Destroy'),
                            classname: 'btn btn-xs btn-danger btn-ajax  btn-destroyit',
                            icon: 'fa fa-times',
                            url: 'facrm/customer/index/destroy',
                            refresh: true
                        }
                    ],
                    formatter: Table.api.formatter.operate
                }
            ];

            if (Config.fields && Object.keys(Config.fields).length > 0) {
                var fields = JSON.parse(Config.fields);
                var start = 20;
                for (var i = 0; i < fields.length; i++) {
                    columnss.splice(start + i, 0, fields[i]);
                }
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    columnss
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                return false;
            });
        },
        share: function () {
            Controller.api.bindevent();
        },
        recover: function () {
            Controller.api.bindevent();
        },
        deal: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                if ("undefined" == typeof customer_id) {
                    customer_id = 0;
                }
                Fast.api.ajax({
                    type: 'GET',
                    url: 'facrm/fields/get_fields_html',
                    data: {source: 'customer', 'id': customer_id,'tpl':'fields_6_2'}
                }, function (data) {
                    $("#extend").html(data.html);
                    Form.api.bindevent($("#extend"));
                    return false;
                });
                require.config({
                        paths: {
                            'tagsinput': '../addons/facrm/js/jquery.tagsinput',
                            'autocomplete': '../addons/facrm/js/jquery.autocomplete',
                        },
                        shim: {
                            'autocomplete': {
                                deps: ['jquery'],
                                exports: '$.fn.extend'
                            },
                            'tagsinput': {
                                deps: ['jquery', 'autocomplete', 'css!../addons/facrm/css/jquery.tagsinput.min.css'],
                                exports: '$.fn.extend'
                            }
                        }
                    }
                );
                require(['tagsinput'], function () {
                    //标签输入
                    var elem = "#c-tags";
                    var tags = $(elem);
                    tags.tagsInput({
                        width: 'auto',
                        defaultText: '输入后空格确认',
                        minInputWidth: 110,
                        height: '36px',
                        placeholderColor: '#999',
                        onChange: function (row) {
                            if (typeof callback === 'function') {

                            } else {
                                $(elem + "_addTag").focus();
                                $(elem + "_tag").trigger("blur.autocomplete").focus();
                            }
                        },
                        autocomplete: {
                            url: 'facrm/tag/autocomplete?type=customer',
                            minChars: 1,
                            menuClass: 'autocomplete-tags'
                        }
                    });
                });

               //云呼
                $(document).on('click', ".cloudcall", function () {
                    var that = this;
                    var type = $(that).data('type');
                    var typeid = $(that).data('typeid');
                    var name = $(that).data('name');
                    var field = $(that).data('field');

                    Fast.api.ajax({
                        url: 'facrm/setting/cloudcall/call',
                        data: {type: type, typeid: typeid,field:field,name:name}
                    }, function (data) {
                        Toastr.success("呼叫成功!");
                        return false;
                    });
                });


            },
            formatter: {
                thumb: function (value, row, index) {
                    if (!value) return '';
                    value = value === null ? '' : value.toString();
                    var arr = value.split(',');
                    var html = [];
                    $.each(arr, function (i, value) {

                        var reg = /\.(png|jpg|gif|jpeg|webp)$/;
                        if (reg.test(value)) {
                            html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img src="' + Fast.api.cdnurl(value) + '" alt="" style="max-height:90px;max-width:120px"></a>');
                        } else {
                            if (row.imagetype == undefined) row.imagetype = /[^\.]+$/.exec(value);
                            html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>');
                        }
                    });
                    return html.join(' ');
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                },
                filename: function (value, row, index) {
                    return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
            }
        }
    };
    return Controller;
});